// 菜单请求工具类封装
import {getRequest} from "./api";

export const initMenu=(router,store)=>{
    if(store.state.nav.length > 0){
        return;
    }
    getRequest("/account/SysLogin/query/CurrentUserAccessibleMenu").then(data=>{
        if (data){
            let fmtRoutes = formatRoutes(data.data);
            // 减少请求，数据缓存
            store.dispatch('SETNAV',fmtRoutes);
            router.addRoutes(fmtRoutes);
        }
    })
}
// 路由拼接
function formatRoutes(routes){
    let fmRoutes = [];
    for (const key in routes) {
        if (routes.hasOwnProperty.call(routes, key)) {
            const router = routes[key];
            let component = router.componentName;
            let path = router.directoryPath;
            let name = router.menuName;
            let {
                menuIcon,
                children
            } = router;
            if (children && children instanceof Array){
                children = formatRoutes(children);
            }
            let fmRouter = {
                path:path,
                name:name,
                menuIcon:menuIcon,
                children:children,
                component(resolve){
                    if (component.startsWith('Home')){
                        require(['../src/components/Home.vue'],resolve);
                    }else if(component.startsWith('BtAccount')){
                        require(['../src/views/account/'+component+'.vue'],resolve);
                    }else if(component.startsWith('System')){
                        require(['../src/views/system/'+component+'.vue'],resolve);
                    }else if(component.startsWith('Form')){
                        require(['../src/views/activity/'+component+'.vue'],resolve);
                    }else if(component.startsWith('workflow')){
                        require(['../src/views/activity/'+component+'.vue'],resolve);
                    }else if(component.startsWith('office')){
                        require(['../src/views/activity/'+component+'.vue'],resolve);
                    }
                }
            } 
            fmRoutes.push(fmRouter);
        }
        }
        return fmRoutes;
}